package csc.daonjpa.java.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;



import csc.daonjpa.java.dao.CardDAO;
import csc.daonjpa.java.domain.Card;

@Controller
public class CardController {
	
	@RequestMapping(value = "/card")
	public String getCardList(Model model) {
		// Create a new AccountDAO
		CardDAO cardDao = new CardDAO();
		
		// Get the list of all accounts from DB
		List<Card> cardList = cardDao.getCards();
		
		//Add the list of accounts to request object which will be then used in jsp to show to user
		model.addAttribute("listCard", cardList);
		return ("card");
	}
	@RequestMapping(value="/addCard")
	public String addCard(HttpServletRequest request, Model model){
		HttpSession session = request.getSession(true);
		String loginId = (String)session.getAttribute("username");
		String cardType = request.getParameter("cardType");
		String cardNumber = request.getParameter("cardNumber");
		String available = request.getParameter("availableAmount");
		double availableAmount = Double.parseDouble(available);
		Card card = new Card(cardNumber, availableAmount, cardType, loginId);
		// Create an CardDAO
		CardDAO cardDao = new CardDAO();
		
		// Save card to DB
		cardDao.addCard(card);
		return "forward:/card.html";
	}
}
